#!/bin/sh

PREPARE=$1
LANG="$2"
CVERSION=4.6.1
VERSION="$(cat data/eversion)"
CFG="tmp/core17.cfg"
ISODIR="/mnt/sda2/core17"
TESTDIR="/mnt/sda1/boot/iso"
THISDIR=`pwd`
CDLOCATION=/mnt/sda2/distro/Core-$CVERSION.iso
EZTMPDIR=/mnt/sda2/ezremaster
TCE17=/mnt/sda2/tce17

[ "$LANG" ] && LANG=_$LANG
if [ ! -f /usr/local/tce.installed/mkisofs-tools ]; then
   if [ ! -e /etc/sysconfig/tcedir/optional/mkisofs-tools.tcz ]; then
      tce-load -w mkisofs-tools
   fi
      tce-load -is mkisofs-tools
fi
if [ ! -f /usr/local/tce.installed/advcomp ]; then
   if [ ! -e /etc/sysconfig/tcedir/optional/advcomp.tcz ]; then
      tce-load -w advcomp
   fi
      tce-load -is advcomp
fi
if [ ! "$PREPARE" = "-np" ];then
	[ -f tmp/allextensions.lst ] && sudo rm	   tmp/allextensions.lst    
	cp  data/instlist/standard.instlist    	   tmp/allextensions.lst    
#	cat data/instlist/ondemand.instlist  	>> tmp/allextensions.lst
#	cat data/instlist/ndiswrapper.instlist  >> tmp/allextensions.lst
#	cat data/instlist/wififirmware.instlist >> tmp/allextensions.lst
#	cat data/instlist/wifi.instlist 	>> tmp/allextensions.lst
#	cat data/instlist/modem.instlist 	>> tmp/allextensions.lst
	echo Xorg-7.6-3d.tcz	  		>> tmp/allextensions.lst
	echo xf86-video-ati.tcz	  		>> tmp/allextensions.lst
#	echo Xvesa.tcz		  		>> tmp/allextensions.lst
	echo "cd_location = $CDLOCATION" 	   	  	  	>  $CFG
	echo "temp_dir = $EZTMPDIR"		  	  	  	>> $CFG
	echo "app_extract_initrd = exquisite.tcz" 	  		>> $CFG
	echo "app_extract_initrd = Xlibs.tcz" 	  		  	>> $CFG
	echo "extract_tcz_script = chroot" 	  		  	>> $CFG
	sed "s/^/app_outside_initrd_onboot = /" tmp/allextensions.lst	>> $CFG
#	echo -n "ezremaster cfg file created.Press Enter key."
#	read gagme
	sudo rm -rf $EZTMPDIR/*
	./remaster.sh $CFG checkimage
	./remaster.sh $CFG extractimage
	#./remaster.sh $CFG bootcode
	sleep 4
#	echo -n "Next apps.Press Enter key."
#	read gagme
	./remaster.sh $CFG apps
fi
sudo rm tmp/onboot/*
mkdir -p tmp/onboot

echo Xorg-7.6-3d.tcz 	    			>  tmp/onboot/onboot.lst
echo xf86-video-ati.tcz     			>> tmp/onboot/onboot.lst
cat  data/instlist/standard.instlist 	>> tmp/onboot/onboot.lst
#echo -n "onboot files created.Press Enter key."
#read gagme
sudo mv $EZTMPDIR/image/cde $EZTMPDIR/image/tce
if [ "$LANG" = "_it" ];then 
   sudo cp data/iso/boot_it.msg 	$EZTMPDIR/image/boot/isolinux/boot.msg
else
   sudo cp data/iso/isolinux.cfg 	$EZTMPDIR/image/boot/isolinux
   sudo cp data/iso/boot.msg 		$EZTMPDIR/image/boot/isolinux
   sudo cp data/iso/boot.msg 		$EZTMPDIR/image/boot/isolinux/f1
   sudo cp data/iso/f2			$EZTMPDIR/image/boot/isolinux/f2
fi
sudo cp data/iso/menu.c32		$EZTMPDIR/image/boot/isolinux/
sudo cp data/iso/isolinux.bin		$EZTMPDIR/image/boot/isolinux/
sudo cp tmp/onboot/*.lst		$EZTMPDIR/image/tce
sudo cp data/scripts/tc-config   	$EZTMPDIR/extract/etc/init.d/
sudo cp data/scripts/e-tc-functions   	$EZTMPDIR/extract/etc/init.d/
sudo cp data/scripts/tce-setup   	$EZTMPDIR/extract/usr/bin/
sudo cp data/scripts/fromISOfile 	$EZTMPDIR/extract/usr/bin/
sudo cp data/scripts/filetool.sh 	$EZTMPDIR/extract/usr/bin/
sudo cp data/scripts/sethostname	$EZTMPDIR/extract/usr/bin/
sudo cp data/scripts/bootlocal.sh	$EZTMPDIR/extract/opt/
sudo cp remaster.sh		 	$EZTMPDIR/extract/usr/local/bin/
sudo cp data/eversion    		$EZTMPDIR/extract/usr/share/doc/tc/erelease.txt
sudo mkdir -p		    		$EZTMPDIR/extract/usr/local/share/exquisite/data/themes/
sudo cp data/core17.edj    		$EZTMPDIR/extract/usr/local/share/exquisite/data/themes/
sudo mkdir -p				$EZTMPDIR/extract//usr/local/share/pixmaps
sudo cp logo/corE.png			$EZTMPDIR/extract/usr/local/share/pixmaps/core.png
sudo cp logo/corE.png			$EZTMPDIR/extract/usr/local/share/pixmaps/logo.png
sudo rm -r 	   			$EZTMPDIR/extract/etc/skel/.e
sudo cp -a skel/.e   			$EZTMPDIR/extract/etc/skel/
sudo rm -r 	   			$EZTMPDIR/extract/etc/skel/.e/e/fileman/thumbnails
sudo rm -r 	   			$EZTMPDIR/extract/etc/skel/Desktop
sudo cp -a skel/Desktop/   		$EZTMPDIR/extract/etc/skel/
sudo cp    skel/.Xdefaults		$EZTMPDIR/extract/etc/skel/
sudo rm -r 	   			$EZTMPDIR/extract/etc/skel/.elementary
sudo cp -a skel/.elementary/		$EZTMPDIR/extract/etc/skel/
sudo cp    skel/.setbackground		$EZTMPDIR/extract/etc/skel/
sudo mkdir -p				$EZTMPDIR/extract/usr/local/lib/enlightenment/modules/wizard/extra_desktops/
sudo mkdir -p				$EZTMPDIR/extract/usr/local/share/enlightenment/data/icons/
cd 					$EZTMPDIR/extract/usr/local/share/enlightenment/data/icons/
sudo ln -s video_player.png mplayer.png
cd "$THISDIR"
sudo cp data/mplayer-interminal		$EZTMPDIR/extract/usr/local/bin
sudo chmod +x 				$EZTMPDIR/extract/usr/local/bin/mplayer-interminal
sudo mkdir -p				$EZTMPDIR/extract/usr/local/share/applications
sudo cp data/mplayer.desktop		$EZTMPDIR/extract/usr/local/share/applications
sudo cp data/mplayer-audio.desktop	$EZTMPDIR/extract/usr/local/share/applications
sudo cp data/mplayer.desktop		$EZTMPDIR/extract/usr/local/lib/enlightenment/modules/wizard/extra_desktops/
sudo cp data/mplayer-audio.desktop	$EZTMPDIR/extract/usr/local/lib/enlightenment/modules/wizard/extra_desktops/
sudo mkdir -p 				$EZTMPDIR/extract/usr/local/etc/enlightenment/
sudo cp data/sysactions.conf		$EZTMPDIR/extract/usr/local/etc/enlightenment/
sudo mkdir -p 				$EZTMPDIR/extract/var/lib/connman/
sudo cp data/settings			$EZTMPDIR/extract/var/lib/connman/
sudo cp data/filetool.lst		$EZTMPDIR/extract/opt/.filetool.lst
sudo cp -a data/ondemand		$EZTMPDIR/image/tce
sudo rm					$EZTMPDIR/image/boot/core.gz
#sudo cp data/.xsession    		$EZTMPDIR/extract/etc/skel/
#sudo cp data/entrance.conf    		$EZTMPDIR/extract/usr/local/etc/entrance.conf
#sleep 4
#echo -n "Next step package. Press Enter key."
#read gagme
./remaster.sh.nocompress $CFG package
cp $EZTMPDIR/ezremaster.iso "$TESTDIR"
sudo rm 			       $TCE17/onboot*
cp      $EZTMPDIR/image/tce/onboot*    $TCE17/
sudo cp $EZTMPDIR/image/boot/core17.gz $TCE17/boot/
sudo cp $EZTMPDIR/image/boot/vmlinuz   $TCE17/boot/
sudo chgrp -R staff 	 $EZTMPDIR/image/tce/optional
sudo chmod "g+w" -R 	 $EZTMPDIR/image/tce/optional
sudo chgrp -R staff 	 $EZTMPDIR/image/tce/ondemand
sudo chmod "g+w" -R 	 $EZTMPDIR/image/tce/ondemand

